#ifndef _EVENTS_H
#define _EVENTS_H

class Handler
{
private:
    SDL_Event event;
    
    int mouseX;
    int mouseY;
    
    bool *mouseBeen;
    bool *mouseNow;
    
    bool misc[1];
    
    Uint8 *lastKey;
    Uint8 *currentKey;
public:
    Handler();
    
    SDL_Event getEvent();
    
    int getMouseX();
    int getMouseY();
    
    bool getKeyPressed(SDLKey);
    bool getKeyReleased(SDLKey);
    
    bool getMousePressed(int);
    bool getMouseReleased(int);
    
    bool getMiscEvent(int);
    
    void step();
};

Handler::Handler()
{
    lastKey=SDL_GetKeyState(NULL);
    currentKey=lastKey;
    SDL_Event loop;
    while(SDL_PollEvent(&loop))
    {
        if(loop.type==SDL_MOUSEMOTION)
        {
            mouseX=event.motion.x;
            mouseY=event.motion.y;
        }
        if(loop.type==SDL_MOUSEBUTTONDOWN)
        {
            if(loop.button.button==SDL_BUTTON_LEFT)
            {
                mouseNow[0]=true;
            }
            if(loop.button.button==SDL_BUTTON_RIGHT)
            {
                mouseNow[1]=true;
            }
        }
    }
}

SDL_Event Handler::getEvent()
{
	return event;
}

int Handler::getMouseX()
{
    return mouseX;
}

int Handler::getMouseY()
{
    return mouseY;
}

bool Handler::getKeyPressed(SDLKey key)
{
    if (currentKey[key])
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Handler::getKeyReleased(SDLKey key)
{
    if(!currentKey[key] && lastKey[key])
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Handler::getMousePressed(int btn)
{
    if (mouseNow[btn])
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Handler::getMouseReleased(int btn)
{
    if (!mouseNow[btn] && mouseBeen[btn])
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Handler::getMiscEvent(int msc)
{
    if(misc[msc])
    {
        return true;
    }
    else
    {
        return false;
    }
}

void Handler::step()
{
    mouseBeen=mouseNow;
    lastKey=currentKey;
    currentKey=SDL_GetKeyState(NULL);
    SDL_Event loop;
    loop=getEvent();
    while (SDL_PollEvent(&loop)) 
    {
            //Miscelaneous Events
         if(loop.type==SDL_QUIT)
         {
             misc[X_OUT]=true;
         }
            //Mouse Events
         if(loop.type==SDL_MOUSEBUTTONDOWN)
         {
             if(loop.button.button=SDL_BUTTON_LEFT)
             {
                 mouseNow[0]=true;
             }
             if(loop.button.button=SDL_BUTTON_RIGHT)
             {
                 mouseNow[1]=true;
             }
         }
         if(loop.type==SDL_MOUSEMOTION)
         {
             mouseX=event.motion.x;
             mouseY=event.motion.y;
         }
         if(loop.type==SDL_MOUSEBUTTONUP)
         {
             if(loop.button.button=SDL_BUTTON_LEFT)
             {
                 mouseNow[0]=false;
             }
             if(loop.button.button=SDL_BUTTON_RIGHT)
             {
                 mouseNow[1]=false;
             }
         }   
    }
}

#endif
